package com.itzhanghada.rebitmq.Controller;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tags;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class MetricsCollector {
    private final MeterRegistry meterRegistry;
    
    public MetricsCollector(MeterRegistry meterRegistry) {
        this.meterRegistry = meterRegistry;
    }
    
    public void recordTransaction(String serviceName, String status, long duration) {
        Tags tags = Tags.of("service", serviceName, "status", status);
        meterRegistry.timer("rebit.transaction", tags).record(duration, TimeUnit.MILLISECONDS);
    }
    
    public void recordMessageSent(String exchange, String routingKey, boolean success) {
        Tags tags = Tags.of("exchange", exchange, "routingKey", routingKey, "success", String.valueOf(success));
        meterRegistry.counter("rebit.message.sent", tags).increment();
    }
    
    public void recordMessageConsumed(String queue, boolean success) {
        Tags tags = Tags.of("queue", queue, "success", String.valueOf(success));
        meterRegistry.counter("rebit.message.consumed", tags).increment();
    }
    
    public void recordCompensation(int count) {
        meterRegistry.counter("rebit.compensation.count").increment(count);
    }
}